<?php

$option = $_POST["option"];
$params = $_POST["params"];
$params2 = $_POST["params2"];
require '../../../DB/db_data_loader.php';
require '../../../Init/database.php';
DB::connect();

switch ($option) {
    case 'saveApp':
        updateApp($params, $params2);
        break;
    case 'saveUsed':
        updateUsedIS($params, $params2);
        break;
    case 'saveReceived';
        updateReceivedApps($params, $params2);
        break;
}

function updateApp($params, $params2) {
    // Getting all param rows
    $param_array = explode("::", $params);
    unset($param_array[count($param_array) -1]);
    $param_array2 = explode("::", $params2);
    unset($param_array2[count($param_array2) -1]);
    
    // For each row get data and make DB query
    foreach($param_array as $param => $row) {
        $row_data = explode(",", $row);
        unset($row_data[count($row_data) - 1]);
        $measure_data = explode(":", $row_data[0]);
        $measure = $measure_data[1];
        unset($row_data[0]);
        
        foreach($row_data as $administration) {
            $administration_data = explode(":", $administration);
            $subdivision = $administration_data[0];
            $time = $administration_data[1];
            // Sending Cell data to update DB
            if ($time == "") {
                doDeleteAppQuery($measure, $subdivision);
            } else {
                doUpdateAppQuery($measure, $subdivision, $time);
            }            
        }
    }
}

function doUpdateAppQuery($measure, $subdivision, $time) {
    $query = "INSERT INTO `support_administration` VALUES('" . $measure . "','" . $subdivision . "'," . $time . ",'" . date("Y/m/d") . "') ON DUPLICATE KEY UPDATE time=" . $time;
    mysql_query($query);
    print_r(mysql_error());
}

function doDeleteAppQuery($measure, $subdivision) {
    $query = "DELETE FROM `support_administration` WHERE measure_code='" . $measure . "' and subdivision_code='" . $subdivision . "'";
    mysql_query($query);
    print_r(mysql_error());
}

function updateUsedIS($params, $params2) {
    // Getting all param rows
    $param_array = explode("::", $params);
    unset($param_array[count($param_array) -1]);

    // For each row get data and make DB query
    foreach($param_array as $param => $row) {        
        $row_data = explode(",", $row);
        unset($row_data[count($row_data) - 1]);
        $is_data = explode(":", $row_data[0]);
        $is = $is_data[1];
        
        unset($row_data[0]);
        foreach($row_data as $using) {
            $used_by = explode(":", $using);
            $subdivision = $used_by[0];
            $used = $used_by[1];
            // Sending Cell data to update DB
            if ($used != "1" and $used != "checked") {
                doDeleteUsedQuery($is, $subdivision);
            } else {
                doUpdateUsedQuery($is, $subdivision);
            }
        }
    }
}

function doUpdateUsedQuery($is, $subdivision) {
    $query = "INSERT INTO `is_subdivisions` VALUES ('" . $is . "','" . $subdivision . "','" . date("Y/m/d") . "') ON DUPLICATE KEY UPDATE date=date";
    mysql_query($query);
    print_r(mysql_error());
}

function doDeleteUsedQuery($is, $subdivision) {
    $query = "DELETE FROM `is_subdivisions` WHERE is_code='" . $is . "' and subdivision_code='" . $subdivision . "'";
    mysql_query($query);
    print_r(mysql_error());
}

function updateReceivedApps($params, $params2) {
    // Getting all param rows
    $param_array = explode("::", $params);
    unset($param_array[count($param_array) -1]);
    
    // For each row get data and make DB query
    foreach($param_array as $param => $row) {        
        $row_data = explode(",", $row);
        unset($row_data[count($row_data) - 1]);
        $measure_data = explode(":", $row_data[0]);
        $measure = $measure_data[1];
        
        unset($row_data[0]);
        foreach($row_data as $received_on) {
            $received_data = explode(":", $received_on);
            $date = $received_data[0];
            $received = $received_data[1];
            // Sending Cell data to update DB
            if ($received == "") {
                doDeleteReceivedQuery($date, $measure);
            } else {
                doUpdateReceivedAppsQuery($date, $measure, $received);
            }
        }
    }
}

function doUpdateReceivedAppsQuery($date, $measure, $received) {
    $query = "INSERT INTO `historical_quantities` VALUES ('" . $date . "','" . $measure . "','" . $received . "') ON DUPLICATE KEY UPDATE administrated=" . $received;
    mysql_query($query);
    print_r(mysql_error());
}

function doDeleteReceivedQuery($date, $measure) {
    $query = "DELETE FROM `historical_quantities` WHERE date='" . $date . "' and measure_code='" . $measure . "'";
    mysql_query($query);
    print_r(mysql_error());
}
?>